package com.teamwings.ctrls.formCtrls.noField;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.teamwings.common.aspect.annotation.FormControl;
import com.teamwings.common.constant.FormConstant;
import com.teamwings.common.vo.Result;
import com.teamwings.ctrls.FormComponent;
import com.teamwings.modules.design.vo.FormVo;
import com.teamwings.modules.page.service.IPageService;
import com.teamwings.util.VoUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@FormControl(type = "print_button", needField = false)
@Data
public class PrintButton extends FormComponent {

    public PrintButton() {
        super();
        this.type = "print_button";
    }

    @Override
    public Result<?> trigger(
            FormConstant.TRIGGER_TYPE type,
            IPageService pageService,
            FormVo formDesignCode,
            Object paramsData
    ) {
        JSONObject jo = JSON.parseObject(paramsData + "");
        // 打印相关方法
        if (type == FormConstant.TRIGGER_TYPE.CTRL_HANDLER) {
            String key = jo.getString("key");
            String formId = jo.getString("formId");
            List<Map> data = this.getData(pageService, key, formId);
            return Result.OK(data);
        }
        return Result.error("参数有误，查询失败。");
    }

    private List<Map> getData(IPageService pageService, String key, String formId) {
        String tableCode = pageService.getTableCodeByFormId(formId);
        FormVo formVo = VoUtils.newFormVoAllowNoField(tableCode);
        List<FormComponent> formComponentList = formVo.getList();
        String sql = "";
        String tableType = "";
        String tableName = "";
        ArrayList<String> columns = new ArrayList<>();

        // 和jeecgboot生成的查询构造器的sql语句进行拼接
        for (FormComponent formComponent : formComponentList) {
            // 后期这里需要缓存一下，logicTable的key对应那个sql，这样不需要每次查询的时候促发一次table_code转formVo
            if (formComponent.getKey().equals(key)) {
                Map<String, Object> options = formComponent.getOptions();
                sql = String.valueOf(options.get("sql"));
                tableType = String.valueOf(options.get("tableType"));
                tableName = String.valueOf(options.get("tableName"));
                if (options.get("columns") instanceof JSONArray) {
                   JSONArray columnsJSONArr = (JSONArray) options.get("columns");
                    for (Object o : columnsJSONArr) {
                        String column = String.valueOf(o);
                        columns.add(column);
                    }
                }
                break;
            }
        }

        List<Map> maps = new ArrayList<>();
        if (tableType.equals(FormConstant.LOGIC_TABLE_TYPE.SQL.getText())) {
            // 直接查询
            maps = pageService.executeSql(sql);
        } else if (tableType.equals(FormConstant.LOGIC_TABLE_TYPE.SINGLE_TABLE.getText())) {
            maps = pageService.selectFields(StringUtils.join(columns, ","), tableName);
        } else {
            // 报错，提示逻辑表格未设置
        }

        return maps;
    }

}
